// pages/comment/commentIndex.js
import util from "../../utils/util"
wx.cloud.init({
  evn: "cloud1-0gmoyy4g42aefbe5"
})
const DB = wx.cloud.database().collection('menu_home')
let menuInfo
let openInfo
let pageNo
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuName: '',
    menuImg: '',
    menuId: '',
    desc: '',
    inputContent: '',
    inputScore: 0,
    flag: true,
    // 灰
    zanImg: '../../image/fullX.png',
    // 黄
    zan_gImg: '../../image/full_gX.png',
    openIdInfo: '',
    detailInfo: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    menuInfo = options
    this.setData({
      menuName: options.menuName,
      menuImg: options.menuImg,
      desc: options.desc,
      menuId: options.menuId
    })
    wx.cloud.callFunction({ name: 'getOpenId' }).then(res => { openInfo = res.result })
  },
  // 发布评论
  commentBtn() {
    DB.doc(menuInfo.menuId).get().then(res => {
      const whereData = res.data.comment
      let nowData = {
        desc: this.data.inputContent,
        commentTime: util.format(new Date())
      }
      let flag = false
      let indexVal = 0
      whereData.map((item, index) => {
        if (item.openId && item.openId === openInfo.openid) {
          flag = true
          indexVal = index
        }
      })
      if (flag) {
        whereData[indexVal].childComment.unshift(nowData)
      } else {
        const newNowData = {
          openId: openInfo.openid,
          userName: "用户名",
          childComment: [{
            desc: this.data.inputContent,
            commentTime: util.format(new Date())
          }]
        }
        whereData.push(newNowData)
      }
      wx.showLoading({
        title: '发送中...',
      })
      wx.cloud.callFunction({
        name: "updateData",
        data: {
          comment: whereData,
          menuId: menuInfo.menuId
        }
      }).then(res => {
        wx.showLoading({
          title: '发布成功',
        })
        setTimeout(() => {
          wx.hideLoading()
          wx.switchTab({
            url: '/pages/home/homeIndex',
          })
        }, 2000)
      }).catch(err => {
      })
    }).catch(err => {
      wx.hideLoading()
    })
  },
  cancel() {
    wx.navigateBack()
  },
  // 获取详情及评价
  getDetail(pageNo) {
    wx.showLoading({
      title: '加载中。。。',
    })
    wx.cloud.callFunction({
      name: 'getDetail',
      data: {
        pageNo: 10 * pageNo, //第几条开始
        pageSize: 10, //每页加载多少条
        database: 'menu_home', //动态配置需要的数据库名称
        menuId: this.data.menuId
      }
    }).then(res => {
      wx.hideLoading()
      res.result.data.map(itemF => {
        itemF.fabulous.map(itemFF => {
          if (itemFF.openId === this.data.openIdInfo.openid) {
            itemFF.childFabulous.map(itemFFF => {
              itemF.isFabulousNew = itemFFF.isFabulous
            })
          }
        })
      })
      this.setData({
        detailInfo: res.result.data,
      })
    })
  },
  // 点赞
  dianZan(e) {
    e.target.dataset.info.isFabulousNew = !e.target.dataset.info.isFabulousNew
    if (e.target.dataset.info.isFabulousNew) {
      e.target.dataset.info.fabulousNum += 1
    } else {
      e.target.dataset.info.fabulousNum -= 1
    }
    this.data.detailInfo.map(item => {
      if (item._id === e.target.dataset.info._id) {
        item.isFabulousNew = e.target.dataset.info.isFabulousNew
        item.fabulousNum = e.target.dataset.info.fabulousNum
      }
    })
    this.setData({
      detailInfo: this.data.detailInfo
    })
    DB.doc(e.target.dataset.info._id).get().then(res => {
      const whereData = res.data.fabulous
      let flag = false
      let indexVal = 0
      whereData.map((item, index) => {
        if (item.openId && item.openId === this.data.openIdInfo.openid) {
          flag = true
          indexVal = index
        }
      })
      if (flag) {
        whereData[indexVal].childFabulous[0].dianzanTime = util.format(new Date())
        whereData[indexVal].childFabulous[0].isFabulous = e.target.dataset.info.isFabulousNew
      } else {
        const newNowData = {
          openId: this.data.openIdInfo.openid,
          childFabulous: [{
            isFabulous: e.target.dataset.info.isFabulousNew,
            dianzanTime: util.format(new Date())
          }]
        }
        whereData.push(newNowData)
      }
      wx.cloud.callFunction({
        name: 'updataFabulous',
        data: {
          fabulous: whereData,
          fabulousNum: e.target.dataset.info.isFabulousNew ? res.data.fabulousNum += 1 : res.data.fabulousNum -= 1,
          menuId: e.target.dataset.info._id
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    pageNo = 0
    this.getDetail(pageNo)
    this.getOpenId()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 文本输入框
  bindTextAreaBlur(e) {
    this.setData({
      inputContent: e.detail.value
    })
  },
  getOpenId() {
    wx.cloud.callFunction({
      name: 'getOpenId'
    }).then(res => {
      this.setData({
        openIdInfo: res.result
      })
    })
  },
})